package com.mingchuang.administrator.mingchuang;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import com.mingchuang.administrator.mingchuang.pile.UploadPileActivity;
import com.mingchuang.administrator.mingchuang.service.ReceiveFileService;
import com.mingchuang.administrator.mingchuang.wifi.WifiUtil;

/**
 * Created by Administrator on 2016/1/18.
 * 创建wifi 热点 开始接收文件
 */
public class WifiApActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private ToggleButton wifiSwitchBtn;

    private WifiManager wifiManager;


    private Button mainBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wifiap_activity);

        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);


        initView();
    }

    private void initView() {

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("设备连接");


        wifiSwitchBtn = (ToggleButton) findViewById(R.id.wifi_btn);
        wifiSwitchBtn.setOnCheckedChangeListener(this);


        mainBtn = (Button) findViewById(R.id.mian_btn);
        mainBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.mian_btn://待上传基桩列表页面

                Intent service = new Intent("mingchuang.receive.file");
                service.setClass(this,ReceiveFileService.class);
                startService(service);

                Intent intent = new Intent();
                intent.setClass(this,UploadPileActivity.class);
                startActivity(intent);



                break;

            default:
                break;
        }





    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


        if (isChecked) {
            //打开WifiAp
            WifiUtil.stratWifiAp(wifiManager);


            //获取本机IP地址并显示

            WifiUtil.getIpAdddress(wifiManager);

        } else {
            //未选中

            WifiUtil.closeWifiAp(wifiManager);
        }
    }
}
